/////////////////////////////////////////////////////////////////
//File: ArticleInfo.cs
//Description: 
//  公用Header，本页是个生成html碎片的完整aspx页面，请使用Server.Execute("url")调用。
//  
//Copyright© 2011 Moliu
//History:
//  2011-2-23 23:28:31    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using MAF.Common;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Service;
using My.Blog.Business.Security;
using My.Blog.Web.Controllers;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;
using System.Text.RegularExpressions;


namespace My.Blog.Web.UI_MAF_Template
{
    public class Header : My.Blog.Web.Controllers.BasePage
    {
        public Header() 
        { 
            HttpContext context = HttpContext.Current;

            // 标签
            const string IF_LOGON = "If:Logon";
            const string IF_NOT_LOGON = "IfNot:Logon";
            const string IF_BLOGMASTER = "If:BlogMaster";
            const string IF_MAINTAIN = "If:Maintain";
            const string IF_NOT_MAINTAIN = "IfNot:Maintain";

            // 模板路径及逻辑标签正则表达式
            string TPL_FILE_PATH = ConfigurationManager.AppSettings["TemplatesPath"] + @"/_Header.html";
            const string REG_LOGIC = @"(?is)<(Tag>)(.*?)</\1";

            // 模板处理对象
            string tpl = File.ReadAllText(context.Server.MapPath(TPL_FILE_PATH));
            Template tplLogic = new Template(tpl, REG_LOGIC);
            Template tplEntity;
            NameValueCollection mapLogic = new NameValueCollection();
            NameValueCollection mapEntity = new NameValueCollection();
            mapEntity.Add( "{RootUrl}",this.RootUrl );

            // 数据源
            IBlog _Blog = BizFactory.Instance.CreateBlog();
            
            // 进入，退出管理模式。
            if (true == this.IsBlogMaster)
            {
                string op = context.Request.QueryString["Op"];
                if ("ExitMasterMode" == op && this.IsMasterMode)
                    this.ExitMasterMode();
                else if ("EntryMasterMode" == op && !this.IsMasterMode)
                    this.EntryMasterMode();
            }

            BlogInfo _Info = null;
            if (SecurityUtility.IsLogon)
            {
                int id; int.TryParse(SecurityUtility.UserId, out id);
                _Info = new BlogInfo();
                _Info.Id = id;
                _Info = _Blog.ReadBlogByConditions(_Info);
                
                mapLogic.Add(IF_NOT_LOGON, "");        
                mapEntity.Add( "{Owner}",_Info.Owner );

                // 进入退出管理模式按钮
                if (true == this.IsBlogMaster)
                {
                    string query = string.IsNullOrEmpty(context.Request.Url.Query) ? "?" : (context.Request.Url.Query + "&");
                    query = Regex.Replace(query, "Op=ExitMasterMode", "");
                    query = Regex.Replace(query, "Op=EntryMasterMode", "");
                    query = query.TrimEnd('&');
                    if (true == this.IsMasterMode)
                        mapLogic.Add(IF_NOT_MAINTAIN, "");
                    else
                        mapLogic.Add(IF_MAINTAIN, "");
                    mapEntity.Add("{Query}", query);
                }
                else
                {
                    mapLogic.Add(IF_BLOGMASTER, "");
                }         
            }
            else
            {
                mapLogic.Add(IF_LOGON, "");
                mapLogic.Add(IF_BLOGMASTER, "");
            }
            
            //执行模板替换
            tplLogic.Replace(mapLogic);
            tplEntity = new Template(tplLogic.Result);
            tplEntity.Replace(mapEntity);
            context.Response.Write(tplEntity.Result);

        }
    }
}